{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4901ec48",
   "metadata": {},
   "source": [
    "### 1.函数定义的基本形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf16f6d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def funcname(arg1, arg2,... argN):#注意也是有冒号和缩进的\n",
    "    \n",
    "    ''' Document String'''\n",
    "\n",
    "    statements\n",
    "\n",
    "    return <value>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fbb78c39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "please enter your name:freddy\n",
      "hello freddy\n",
      "how do you do\n"
     ]
    }
   ],
   "source": [
    "def func(username):\n",
    "    print(\"hello \"+username)\n",
    "    print(\"how do you do\")\n",
    "name=input(\"please enter your name:\")\n",
    "func(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "360b1375",
   "metadata": {},
   "source": [
    "在定义完成之后可以用help([object])函数来查看这个部分的功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5c37aabe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function func in module __main__:\n",
      "\n",
      "func(username)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(func)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0c69222",
   "metadata": {},
   "source": [
    "### 2.return 返回语句"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0964fa87",
   "metadata": {},
   "source": [
    "在执行完函数的功能之后，需要输出一个结果，这时就要return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f91097db",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function times in module __main__:\n",
      "\n",
      "times(x, y)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def times(x,y):\n",
    "    z=x*y\n",
    "    return z"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "363df0a9",
   "metadata": {},
   "source": [
    "return也可以同时返回多个变量,默认返回的形式是元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "37ed5ca4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(100, 6, 10, 100)\n"
     ]
    }
   ],
   "source": [
    "eglist = [10,50,30,12,6,8,100]\n",
    "def egfunc(eglist):\n",
    "    highest = max(eglist)\n",
    "    lowest = min(eglist)\n",
    "    first = eglist[0]\n",
    "    last = eglist[-1]\n",
    "    return highest,lowest,first,last\n",
    "print(egfunc(eglist))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65b89938",
   "metadata": {},
   "source": [
    "### 3.隐式参数\n",
    "\n",
    "如果有些参数经常是固定的，可以定义成常数形式，如果不赋值就是默认值，赋值则改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "21d9ac96",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "14\n"
     ]
    }
   ],
   "source": [
    "def implicit(x,add=5):\n",
    "    return x+add\n",
    "print(implicit(4))#没有给第二个赋值，默认加5\n",
    "print(implicit(4,10))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c903031e",
   "metadata": {},
   "source": [
    "### 4.任意数量参数（参数的数量未知）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2b88db51",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 5, 8, 7]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def add_n(*num):\n",
    "    list=[]\n",
    "    for i in num:\n",
    "        list.append(i)\n",
    "    print(list)\n",
    "    return sum(list)\n",
    "add_n(1,5,8,7)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "415c355c",
   "metadata": {},
   "source": [
    "### 5.lambda函数 （定义的函数是单次使用的）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c3861fa6",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "(9, 10)\n"
     ]
    }
   ],
   "source": [
    "z=lambda x:x**2\n",
    "print(z(10))\n",
    "z=lambda x,y:(x+y,x*y)\n",
    "print((9,10))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98abcd14",
   "metadata": {},
   "source": [
    "map()可以对列表里面的每一个元素都使用这个函数,得到的结果是一个map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f1bb894d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 16, 27, 4194304, 3125, 46656, 823543, 16777216, 387420489] <class 'map'>\n"
     ]
    }
   ],
   "source": [
    "num1=[1,2,3,4,5,6,7,8,9]\n",
    "num2=[1,4,3,11,5,6,7,8,9]\n",
    "cal=map(lambda x,y:x**y,num1,num2)\n",
    "print(list(cal),type(cal))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bf135e1",
   "metadata": {},
   "source": [
    "filter函数则是可以在lambda中设定条件，然后过滤出对应的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5eae07e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 3]\n"
     ]
    }
   ],
   "source": [
    "cal=filter(lambda x:x<5,num2)\n",
    "print(list(cal))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
